package com.newbee.activity;

import java.util.HashMap;
import java.util.Map;

import com.hhu.newbee.R;
import com.newbee.app.OnRefrushActivity;
import com.newbee.service.MainService;
import com.newbee.service.MainServiceKey;
import com.newbee.service.MainServiceTask;
import com.newbee.service.TaskType;
import com.newbee.util.StringUtil;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class ForgetPassWordActivity extends Activity implements
		OnClickListener, OnRefrushActivity
{

	private EditText mAccount;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_forget_password);
		mAccount = (EditText) this.findViewById(R.id.forget_account);
		this.findViewById(R.id.forget_password_send).setOnClickListener(this);
		this.findViewById(R.id.forget_password_back).setOnClickListener(this);
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.forget_password_back:
		{
			finish();
			break;
		}
		case R.id.forget_password_send:
		{

			String account = mAccount.getText().toString();

			if (account.length() <= 0)
			{
				Toast.makeText(this, "邮箱不能为空", Toast.LENGTH_SHORT).show();
				break;
			}
			else if (StringUtil.isEmailFormat(account))
			{

				Map<String, Object> para = new HashMap<String, Object>();
				para.put(MainServiceKey.EMAIL, mAccount.getText().toString());
				MainServiceTask mst = new MainServiceTask(
						TaskType.FORGET_PASSWORD, para,
						ForgetPassWordActivity.class.getName());
				MainService.addActivityMap(
						ForgetPassWordActivity.class.getName(), this);
				MainService.addTaskQueue(mst);
			}
			else
			{
				Toast.makeText(this, "邮箱格式不正确", Toast.LENGTH_SHORT).show();
				break;
			}
		}
		default:
			break;
		}
	}

	@Override
	public void init()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public RtInfo refresh(int taskID, Object... paras)
	{

		switch (taskID)
		{
		case TaskType.FORGET_PASSWORD:
		{
			boolean resp = (Boolean) paras[0];
			if (resp)
			{
				Toast.makeText(this, "已将您的密码发送到您的邮箱", Toast.LENGTH_LONG).show();
				this.finish();
			}
			else
			{
				Toast.makeText(this, "找回密码发送错误", Toast.LENGTH_LONG).show();
			}

			break;
		}
		default:
			break;
		}

		return null;
	}

	@Override
	protected void onDestroy()
	{
		MainService.removeActivity(ForgetPassWordActivity.class.getName());
		super.onDestroy();
	}

}
